﻿<script>

    var _vm;
    $(function () {
        var vm = new ViewModel();
        _vm = vm;
        vmExtend.call(vm);
        ko.applyBindings(vm);
        vm.Init();
    });

    function ViewModel() {
        var me = this;
        ViewModelBase.call(me);
        vmExtend.call(me);

        me.DeleteUrl = "@this.ContentUrl("~/SystemManage/PropertyMapping/Delete")";
        //me.DataTable = new DataTableBase(me);
        me.Dialog = new Dialog(me);

        //详情页
        me.DetailDialog = new DetailDialog(me);
        me.Detail = function () {
            me.DetailDialog.Open(me.DataTable.SelectedModel(),"详情");
        }
        //////////
    }
    function vmExtend() {
        var me = this;

        me.Init = function () {
            me.LoadModels();
        }

        @*me.LoadModels = function (page) {
            me.DataTable.SelectedModel(null);
            var data = me.SearchModel();
            $ace.get("@this.ContentUrl("~/SystemManage/PropertyMapping/GetModels")", data, function (result) {
                me.DataTable.SetModels(result.Data);
            }
         );
        }*@
        me.LoadModels = function (page) {
            me.DataTable.SelectedModel(null);
            var params = me.SearchModel();
            params.page = page || 1;
            $ace.get("@this.ContentUrl("~/SystemManage/PropertyMapping/GetModels")", params, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
          );
         }
    }
    //详情页对话框设置
    function DetailDialog() {
        var me = this;
        DialogBase.call(me);

        me.OnOpen = function () {
            //将“保存”按钮隐藏
            me.ShowSaveButton(false);
            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);
                me.Model(bindModel);
            }
            else {
                me.Model({});
            }
        }
    }

    function Dialog(vm) {
        var me = this;
        DialogBase.call(me);


        me.OnSave = function () {
            var model = me.Model();

            if (!$('#form1').formValid()) {
                return false;
            }

            if (me.EditModel()) {
                $ace.post("@this.ContentUrl("~/SystemManage/PropertyMapping/Update")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
               );
            }
            else {
                $ace.post("@this.ContentUrl("~/SystemManage/PropertyMapping/Add")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
             );
            }
        }
    }
</script>
